<?php
namespace Home\Controller;
use Think\Controller;

class IndexController extends Controller {
    /**审批主页
     * @author leiying
     */
    public function index(){
	    $this->display();
    }

    /**流程查询
     * @author leiying
     */
    public function process(){
        $data = M("project")->alias("a")
            ->field("a.name,IFNULL(b.content,'') content")
            ->join(C('DB_PREFIX')."process b ON a.id = b.p_id",'left')
            ->order("a.add_time desc")
            ->select();
        $this->assign("data",$data);
        $this->display();
    }

    /**申请查询
     * @author leiying
     */
    public function apply_for(){
        $this->display();
    }

    /**申请查询结果
     * @author leiying
     */
    public function demand(){
        $this->display();
    }

    /**项目申请
     * @author leiying
     */
    public function project(){
        $projects = M("project")->order("add_time desc")->select();
        $this->assign("projects",$projects);
        $provinces = M("region")->field("code,fullName")->where(array("parentCode"=>'100000'))->select();
        $this->assign("provinces",$provinces);
        $this->display();
    }

    /**补充申请
     * @author leiying
     */
    public function supplement(){
        $id = I('post.apply_id',0);
        if(empty($id)){
            $this->error('异常错误，如有疑问请联系客服');
        }
        $projects = M("project")->order("add_time desc")->select();
        $this->assign("projects",$projects);
        $provinces = M("region")->field("code,fullName")->where(array("parentCode"=>'100000'))->select();
        $this->assign("provinces",$provinces);
        $res = M("apply")->alias("a")->field("a.*,b.name project_name")->join(C('DB_PREFIX')."project b ON a.p_id = b.id","left")->where(array("a.id"=>$id))->find();

        if($res){
            $attachment = M("data")->field("name,ext,id")->where(array('p_id'=>$res['p_id']))->find();
            if($attachment){
                $data = array(
                    'path' => U('Home/Data/download',array('id'=>$attachment['id'])),
                    'filename' => $attachment["name"].'.'.$attachment['ext'],
                );
            }else{
                $data = array("path"=>'javascript:void(0)',"filename"=>'');
            }
            $this->assign("attach",$data);
            $this->assign("res",$res);
            $this->display();
        }else{
            $this->error('未查询到申请信息，如有疑问请联系客服');
        }
    }

    /**项目申请下载 页面
     * @author leiying
     */
    public function projectShow(){
        if(IS_POST){
            $id = I('post.apply_id',0);
        }else{
            $token = I('token','');
            if($token == md5("leiying15102775120")){
                $id = I('apply_id',0);
            }else{
                $this->error('异常错误，如有疑问请联系客服',U('Home/Index/index'),3);
            }
        }
        if(empty($id)){$this->error("访问异常，如有疑问请联系客服");}
        $data = M('apply')->alias("a")->field("a.*,b.name project_name")->join(C('DB_PREFIX')."project b ON a.p_id = b.id","left")->where(array('a.id'=>$id))->find();
        $this->assign("data",$data);
        $this->display();

    }

    /**查询进度
     * @author leiying
     */
    public function QueryProgress(){
        $post = I('post.');
        $apply_id = isset($post['apply_id'])?$post['apply_id']:0;
        if(empty($apply_id)){
            $this->error('非法操作，如有疑问请联系客服',U('Home/Index/apply_for'),3);
        }
        $action = isset($post['action'])?$post['action']:'apply_for';
        $this->assign("action",$action);
        $row = M("apply")->where(array("id"=>$apply_id))->find();
        if($row){
            switch($row["status"]){
                case 1:
                    if(strtotime($row['wait_accept_time'].' 23:59:59') < time()){
                        $row["last_time"] = '已超出';
                    }else{
                        $row["last_time"] = floor((strtotime($row['wait_accept_time'].' 23:59:59')-time())/86400).'天';
                    }
                    break;
                case 2:
                case 3:
                case 4:
                    if(strtotime($row['wait_complete_time'].' 23:59:59') < time()){
                        $row["last_time"] = '已超出';
                    }else{
                        $row["last_time"] = floor((strtotime($row['wait_complete_time'].' 23:59:59')-time())/86400).'天';
                    }
                    break;
                case 5:
                    $row["last_time"] = floor(($row["complete_time"] - $row["apply_time"])/86400).'天';
                    break;
                case 6:
                case 7:
                    break;
            }
            $this->assign("res",$row);
            $this->display("demand");
        }else{
            $this->error('未查询到项目,如有疑问请联系客服',U('Home/Index/apply_for'),3);
        }

    }

    /**执行打印
     * @author leiying
     */
    public function do_print(){
        $id = I('get.id');
        if(empty($id)){
            $this->error("异常错误，如有疑问请联系客服",U('Home/Index/index'),3);
        }

        $localhost = $_SERVER [ 'HTTP_HOST' ];
        $a_path = "/Uploads/PDF/Home/";
        $path = $_SERVER['DOCUMENT_ROOT']. $a_path;
        if(!file_exists($path))
        {
            mkdir($path,0777,true);
        }
        $html=$_POST['html'];
        //Turn on output buffering
        ob_start();
        $html='
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>项目申请</title>
    <link rel="stylesheet" href="/Public/Home/extend/css/head.css">
    <link rel="stylesheet" href="/Public/Home/extend/css/demand.css">
    <link rel="stylesheet" href="/Public/Home/extend/css/project.css">
    </head>
<body style="">
'.$html. '</div></div></form></body></html>';
        $filename = M("apply")->where(array("id"=>$id))->getField("number");
        if(!$filename){$this->error("异常错误，如有疑问请联系客服",U('Home/Index/index'),3);}

        if(!file_exists($path."{$filename}.html")){
            file_put_contents($path."{$filename}.html", $html);
        }
        ob_end_clean();
        if(!file_exists($path."{$filename}.pdf")){
//            shell_exec("D:\app\wkhtmltopdf\bin\wkhtmltopdf.exe {$localhost}{$a_path}{$filename}.html {$path}{$filename}.pdf");
            shell_exec("D:\leiying\wkhtmltopdf\bin\wkhtmltopdf.exe {$localhost}{$a_path}{$filename}.html {$path}{$filename}.pdf");
        }

        if(file_exists($path."{$filename}.pdf")){
            header("Content-type:application/pdf");
            header("Content-Disposition:attachment;filename={$filename}.pdf");
            echo file_get_contents("{$path}{$filename}.pdf");
        }else{
            $this->error('异常错误，如有疑问请联系客服',U('Home/Index/index'),3);
            exit;
        }
    }


}